//go:build linux
// +build linux

package main

import (
	"fmt"
	"os/user"
	"time"

	webview "github.com/webview/webview_go"
)

// 获取当前进程运行的用户名
func getCurrentProcessUser() string {
	u, err := user.Current()
	if err != nil {
		return "unknown (error: " + err.Error() + ")"
	}
	return u.Username
}

func main() {
	instance := webview.New(true)
	defer instance.Destroy()
	instance.SetTitle("urmom webview")
	instance.SetSize(800, 600, webview.HintNone)
	instance.Navigate("https://cn.bing.com")

	fmt.Printf("Window pointer: %p\n", instance.Window())
	// 第一个窗口运行后会阻塞，直到窗口关闭
	instance.Run()

	instance2 := webview.New(true)
	defer instance2.Destroy()
	instance2.SetTitle("User Info Example")
	instance2.SetSize(600, 400, webview.HintNone)

	instance2.SetHtml(`
		<h1>Hello World</h1>
		<button onclick="showDate()">Show Current Time</button>
		<button onclick="add(1, 1)">Add 1+1</button>
		<button onclick="gu()">Get Current User</button>
		<p id="userInfo"></p>
		<script>
			async function gu() {
				const user = await getUser()
				document.getElementById('userInfo').textContent = "Current user: " + user
				alert("User: " + user)
			}
		</script>
	`)

	instance2.Bind("showDate", func() {
		fmt.Println("Current time:", time.Now().Format(time.DateTime))
	})
	instance2.Bind("add", func(x, y int) int { // 修改为返回结果
		result := x + y
		fmt.Println("Add result:", result)
		return result
	})
	// 绑定获取实际用户名的函数
	instance2.Bind("getUser", getCurrentProcessUser)

	instance2.Run()
	fmt.Printf("First instance pointer: %p\n", instance)
	fmt.Println("Press F12 to debug (if supported)")
}
